package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.dao.ProductSupplierDao;
import com.hhs.storage.service.ImgService;
import com.hhs.storage.service.ProductSupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.SysUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhs
 * @since 2023-01-04
 */
@Service
@Transactional
public class ProductSupplierServiceImpl extends ServiceImpl<ProductSupplierDao, ProductSupplier> implements ProductSupplierService {
    @Resource
    private ImgService imgService;
    @Override
    public Map<String, Object> getAll(ProductSupplier productSupplier, InputParam inputParam) {
        QueryWrapper ew = new QueryWrapper<ProductSupplier>();
        if(!StringUtils.isEmpty(inputParam.getName())){
            ew.like("name",inputParam.getName());
        }
        ew.orderByDesc("create_time");
        Page<ProductSupplier> result = this.page(new Page<ProductSupplier>(
                inputParam.getPage(),
                inputParam.getSize()),ew);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());
        map.put("code", 1);
        return map;
    }

    @Override
    public Map<String, Object> addOrUpdate(ProductSupplier productSupplier,String deleIdStr, MultipartFile[] qualificationCertificateList) {
        if(StringUtils.isEmpty(productSupplier.getName()) ||
                StringUtils.isEmpty(productSupplier.getPrincipal())||
                StringUtils.isEmpty(productSupplier.getPhone())){
            return ResponseInfo.error("参数错误");
        }
        List<ProductSupplier> tempList = this.list(new QueryWrapper<ProductSupplier>().eq("name", productSupplier.getName()));
        //新增
        if (StringUtils.isEmpty(productSupplier.getId())) {

            productSupplier.setCreateTime(LocalDateTime.now());
            productSupplier.setId(SysUtil.UUID());
            if (tempList.size() > 0) {
                return ResponseInfo.error("名称已存在，请重新输入");
            }
            //资质有图片则保存图片
            if(qualificationCertificateList.length>0){
                imgService.upload( SysUtil.PRODUCT_SUPPLIER_QUALIFICATION_CERTIFICATE,productSupplier.getId(),qualificationCertificateList);
            }
            this.save(productSupplier);
        } else {//修改
            //资质有图片则保存图片
            if(qualificationCertificateList.length>0){
                imgService.upload( SysUtil.PRODUCT_SUPPLIER_QUALIFICATION_CERTIFICATE,productSupplier.getId(),qualificationCertificateList);
            }

            //删除图片
            if(!StringUtils.isEmpty(deleIdStr)){
                String[] deleIdArr = deleIdStr.split(",");
                if(deleIdArr.length>0){
                    for (String id : deleIdArr) {
                        imgService.deleById(id);
                    }
                }
            }
            this.updateById(productSupplier);
        }
        return ResponseInfo.success(null);
    }

    @Override
    public Map dele(String id) {
        if(StringUtils.isEmpty(id)){
            return ResponseInfo.error("参数错误");
        }
        //找到资质的图片，删除图片
        Img img = new Img();
        img.setMainId(id);
        List<Img> imgList = imgService.getByMainId(img);
        for (Img img1 : imgList) {
            imgService.deleById(img1.getId());
        }
        this.removeById(id);
        return ResponseInfo.success(null);
    }
}
